GBML manual / Stacks

stack_pop

Removes AND returns the item at the top of target. If target is already empty, nothing is removed and a plain default value is returned instead (0/0.0/false/an empty string/`nothing`, depending on T) - target never crashes. Check stack_empty(target) first if you need to tell "the top was really this value" apart from "target was empty".

Syntax

stack_pop(target) -> element

Arguments

NameType
targetstack

Returns

element

Description

Removes AND returns the item at the top of target. Everything under it becomes the new top.

If target is already empty, nothing is removed and a plain default value is returned instead (0/0.0/false/an empty string/nothing, depending on target's element type) - target never crashes. Check stack_empty(target) first if the game needs to tell "the top really was this value" apart from "target was empty."

Example

/// @type {stack<int>}
var history = [1, 2, 3];
var top = stack_pop(history); // 3 - history is now [1, 2]

/// @type {stack<int>}
var empty_stack = [];
var fallback = stack_pop(empty_stack); // 0 - empty_stack was empty, so this is the default, not a real popped value
A real capture - the stack with all three moves still in it, and TOP reading 3 before the popA real capture - the stack with all three moves still in it, and TOP reading 3 before the pop
A real capture - the stack with all three moves still in it, and TOP reading 3 before the pop
The same stack after stack_pop(history) - 3 came off the top, POPPED shows what it returnedThe same stack after stack_pop(history) - 3 came off the top, POPPED shows what it returned
The same stack after stack_pop(history) - 3 came off the top, POPPED shows what it returned

On the GBA

the GBA module's own vector back()+pop_back() - O(1), and the empty case is checked before either is called, so this can never read or pop past the end. The default-value fallback is a small compiled-in lambda - no allocation, no new engine primitive, same posture as every other operation on this page.

See also: stack_pushstack_peekstack_lengthstack_emptyData containers